import pymysql, os, threading, re, time
from bs4 import BeautifulSoup

class SqlService():
    def __init__(self):
        self.host = 'localhost'
        self.user = 'root'
        self.pwd = 'root'
        self.database = 'heanny'
        self.db = pymysql.connect(self.host, self.user, self.pwd, self.database)
        self.db.set_charset('utf8')
        print('__init__ success')

    def _Link(self, sql):
        try:
            # self.db.set_charset('utf-8')
            self.cursor = self.db.cursor()
            self.cursor.execute(sql)
            self.db.commit()
            datas = self.cursor.fetchall()
            self.cursor.close()
            return datas
        except Exception as e:
            print('e:', e)

    def _close(self):
        self.db.close()
if __name__ == '__main__':

    aaa = SqlService()
    '''初步转换'''
    # DatasL=aaa._Link("select * from `newbiefly`  ")
    # for data in DatasL[:]:
    #     data = list(data)
    #     html = BeautifulSoup(data[2], 'html.parser')
    #     DivsData = html.find_all('div',attrs={'class':False})
    #     text = DivsData[-1]
        # if len(DivsData)==2 or len(DivsData)==3:
        #     text=DivsData[-1]
        # else:
        #     print('\n\n###################################################\n')
        #     print(len(DivsData),data[-1])
        #     text=DivsData[2]
        # aaa._Link("update `newbiefly` set text ='{}' where id = '{}'".format(text,data[0]))

        # print(len(DivsData))
        # print(text)
        # print('\n\n\n\n################\n\n\n\n')
    '''初步转换结束'''
    '''转换'''
    # for data in aaa._Link("SELECT id,url,text FROM `newbiefly` "):
    for data in aaa._Link("SELECT id,url,html,text FROM `newbiefly` WHERE text = '' and state =0  "):
    # for data in aaa._Link("SELECT id,url,text FROM `newbiefly` where text not like '%登陆兑换%' and text  like '%提取密码%' and text not like '%下载%' "):
        print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$id:{}，url:{}'.format(data[0],data[1]))
        print(data[2])
        # print(data[2])

        # aaa._Link("update `newbiefly` set text ='' where id ='{}'".format(data[0]))
        html = BeautifulSoup(data[2], 'html.parser')
        DivsData = html.find_all('div', attrs={'class': False})
        # DivsData1 = html.find_all('div', attrs={'class': 'entry-content'})

                # aaa._Link("update `newbiefly` set text ='{}',state =1 where id = '{}'".format(DivsData[-1],data[0]))
        ''''''
        # print(DivsData1)





        # print('\n\n\n')
        # print(DivsData1[0])
        # aaa._Link("update `newbiefly` set text = '{}{}',state =1 where id = '{}'".format(DivsData1[0],DivsData[2], data[0]))
        # if (len(DivsData))>=1:
        #     print(DivsData[0])
            # aaa._Link("update `newbiefly` set text = '{}' where id = '{}'".format(DivsData[0], data[0]))
        # for div in DivsData:
        #     print(div)
            # findword = u"(下载)"  # 需要查找的特定中文字符串
            # pattern = re.compile(findword)
            #
            # results = pattern.search(str(div))
            # if results:
            # print(div)
            # ''''''
            # if div.find_all('a', attrs={'target':'_blank'}):
            #     aaa._Link("update `newbiefly` set text = '{}' where id = '{}'".format(div,data[0]))
            # ''''''

        print('\n############################\n\n\n\n\n\n\n\n\n\n\n\n')
    aaa._close()

